package com.stars.easyms.redis.interceptor;

import com.stars.easyms.redis.annotion.RedisDelete;
import com.stars.easyms.redis.exception.RedisInterceptorException;
import org.aopalliance.intercept.MethodInvocation;

import java.util.Set;

/**
 * <p>className: RedisDeleteInterceptor</p>
 * <p>description: RedisDelete注解拦截器</p>
 *
 * @author guoguifang
 * @version 1.2.1
 * @date 2019-03-18 14:25
 */
public class RedisDeleteInterceptor extends AbstractRedisInterceptor<RedisDelete> {

    @Override
    protected Object invoke(MethodInvocation methodInvocation, RedisDelete redisDelete) throws RedisInterceptorException {
        if (isEnabled()) {
            String redisKey = getRedisKey(methodInvocation, redisDelete.group());
            Set<String> hashKeySet = getRedisHashKey(methodInvocation);
            if (hashKeySet.isEmpty()) {
                easyMsRedisTemplate.delete(redisKey);
            } else {
                easyMsRedisTemplate.hashDelete(redisKey, hashKeySet);
            }
        }
        return proceed(methodInvocation);
    }

}